require 'Qt4'
require '/usr/share/hzvirt/hzvault_dd'

class HZVaultNewVM < Qt::Widget
  slots 'create_vm()'
  slots 'finishing()'
  def initialize(parent = nil)
    super
    showMaximized()
    setStyleSheet("HZVaultNewVM {background-color: black;} QLabel { color: #7e7e7e;} QPushButton,QComboBox,QLineEdit {background-color: #101010; color: #7e7e7e;}")

    @lh = Qt::HBoxLayout.new(self)

    #colonne de gauche : visuel + boutons halt et reboot
    @lh_lv1 = Qt::VBoxLayout.new()
    @lh.addLayout(@lh_lv1)
    @tete2mort = Qt::Label.new()
    @tete2mort.setPixmap(Qt::Pixmap.new("/usr/share/hzvirt/head.png"))
    @lh_lv1.addWidget(@tete2mort)
    @lh_lv1_lh = Qt::HBoxLayout.new()
    @lh_lv1.addLayout(@lh_lv1_lh)
    @back = Qt::PushButton.new(tr('Back'), self)
    @lh_lv1_lh.addWidget(@back)
    connect(@back, SIGNAL('clicked()'), self, SLOT('finishing()'))
    
    @lh.addSpacing(200)


    @lh_lv2 = Qt::VBoxLayout.new()
    @lh.addLayout(@lh_lv2)

    @title = Qt::Label.new()
    @title.setText("VM Creator")
    @title.setFixedSize(300,50)
    @title.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
    @lh_lv2.addWidget(@title)

    @lh_lv2_grid = Qt::GridLayout.new()
    @lh_lv2.addLayout(@lh_lv2_grid)

    @label_name = Qt::Label.new(self)
    @label_name.setText("VM Name")
    @label_name.setFixedSize(100,30)
    @lh_lv2_grid.addWidget(@label_name, 0, 0)
    @name = Qt::LineEdit.new(self)
    @name.setFixedSize(300,30)
    @lh_lv2_grid.addWidget(@name, 0, 1)

    @label_os = Qt::Label.new(self)
    @label_os.setText("OS")
    @label_os.setFixedSize(100,30)
    @lh_lv2_grid.addWidget(@label_os, 1, 0)
    @os = Qt::ComboBox.new(self)
    @os.setFixedSize(300,30)
    @os.addItem("Linux")
    @os.addItem("Microsoft Windows")
    @os.addItem("BSD")
    @os.addItem("Solaris")
    @os.addItem("IBM OS/2")
    @os.addItem("Other")
    @lh_lv2_grid.addWidget(@os, 1, 1)

    #Todo add version (with slot on os in order to modify version when os is modified)
#    @label_version = Qt::Label.new(self)
#    @label_version.setText("Version")
#    @lebel_version.setFixedSize(100,30)
#    @layout_vm.addWidget(@label_version, 2, 0)
#    @version = Qt::ComboBox.new(self)
#    @version.setFixedSize(300,30)
#    @version.addItem("

    @label_dd = Qt::Label.new(self)
    @label_dd.setText("HDD size")
    @label_dd.setFixedSize(100,30)
    @lh_lv2_grid.addWidget(@label_dd, 3, 0)
    @dd = HZVaultDD.new(Qt::Horizontal)
    @dd.setFixedSize(300,40)
    @lh_lv2_grid.addWidget(@dd, 3, 1)

    
    @label_lan = Qt::Label.new(self)
    @label_lan.setText("LAN Type")
    @label_lan.setFixedSize(100,30)
    @lh_lv2_grid.addWidget(@label_lan, 4, 0)
    @lan = Qt::ComboBox.new(self)
    @lan.setFixedSize(300,30)
    @lan.addItem("NAT")
    @lan.addItem("Bridged")
    @lan.addItem("No Network")
    @lh_lv2_grid.addWidget(@lan, 4, 1)

    @validate = Qt::PushButton.new(tr('Create the VM'),self)
    @validate.setFont(Qt::Font.new('Times', 12, Qt::Font::Bold))
    connect(@validate, SIGNAL('clicked()'), self, SLOT('create_vm()'))
    @lh_lv2.addWidget(@validate)


  end
  def create_vm
	  #TODO gestion des erreurs
	  system("VBoxManage showvminfo " + @name.text)
	  if ($? != 0)
		  system('VBoxManage createvm -name "' + @name.text  + '" -register')
		  system('VBoxManage setextradata "' + @name.text + '" "GUI/Fullscreen" "on"')
		  system('VBoxManage createhd --filename "/vms/' + @name.text + '.vmdk" --size ' + (@dd.value*1024).to_s + ' --format VMDK --variant Split2G --type normal')
		  #TODO tenir compte de la valeur @lan pour nic1 + nic en hostonly 
		  #system('VBoxManage modifyvm "' + @name.text + '" --memory 256 --acpi on --nic1 nat  --nictype1 82545EM --hda "/vms/' + @name.text + '.vmdk" --bioslogoimagepath /usr/share/hzvirt/bios.bmp --bioslogofadein on --bioslogofadeout on --bioslogodisplaytime 2000')
		  system('VBoxManage modifyvm "' + @name.text + '" --memory 256 --acpi on --bioslogoimagepath /usr/share/hzvirt/bios.bmp --bioslogofadein on --bioslogofadeout on --bioslogodisplaytime 2000')
		  system('VBoxManage modifyvm "' + @name.text + '" --nictype1 82545EM --nic1 nat')
		  if(@os.currentText == "Microsoft Windows")
			  system('VBoxManage storagectl "' + @name.text + '" --name "Disk Controller" --add ide')
		  else
			  system('VBoxManage storagectl "' + @name.text + '" --name "Disk Controller" --add sata')
		  end
	          system('VBoxManage storageattach "' + @name.text + '" --storagectl "Disk Controller" --medium "/vms/' + @name.text + '.vmdk" --type hdd --port 0 --device 0')
		  #AJout d'un controleur IDE pour le DVD
#		  system('VBoxManage storagectl "' + @name.text + '" --name "IDE Controller" --add ide')
		  finishing()
	  end
  end
  def finishing
	  $widget.refresh
	  self.close
  end
end
